var cardScanner = _interopRequireWildcard(require("./card-scanner-util")), _authorize = require("../../utils/authorize");

function _interopRequireWildcard(e) {
    if (e && e.__esModule) return e;
    var t = {};
    if (null != e) for (var n in e) if (Object.prototype.hasOwnProperty.call(e, n)) {
        var a = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(e, n) : {};
        a.get || a.set ? Object.defineProperty(t, n, a) : t[n] = e[n];
    }
    return t.default = e, t;
}

Component({
    properties: {
        file: {
            type: String,
            value: ""
        },
        loading: {
            type: Boolean,
            value: !1
        },
        top: {
            type: String,
            value: "0px"
        },
        mode: {
            type: String,
            value: "single",
            observer: "watchMode"
        }
    },
    data: {
        direction: "vertical",
        albums: []
    },
    ready: function() {
        this.initAccelerometerChange();
    },
    detached: function() {
        wx.stopAccelerometer();
    },
    methods: {
        initAccelerometerChange: function(e) {
            var c = this, s = 0, l = Date.now();
            wx.startAccelerometer(), wx.onAccelerometerChange(function(e) {
                var t = Date.now();
                if (!(t - l < 500)) {
                    var n;
                    l = t;
                    var a = 57.3 * Math.atan2(-e.x, Math.sqrt(e.y * e.y + e.z * e.z)), i = 57.3 * Math.atan2(e.y, e.z);
                    if (50 < a) n = -180 < i && i < -60 || 130 < i ? 1 : s; else if (0 < a && a < 30 || a < 0 && -30 < a) {
                        var r = Math.abs(i);
                        n = 140 < r || r < 40 ? s : i < 0 ? 0 : s;
                    } else n = s;
                    if (n !== s) {
                        var o = 1 === (s = n) ? "horizontal" : "vertical";
                        c.setData({
                            direction: o
                        }), c.triggerEvent("direction", {
                            direction: o
                        }, {});
                    }
                }
            });
        },
        watchMode: function(e) {
            var n = this, t = this.data.albums.length;
            if ("multiple" === e) {
                if (t) return;
                wx.showToast({
                    title: "开启连拍模式",
                    duration: 700
                });
            } else {
                if (!t) return;
                wx.showModal({
                    content: "已拍摄".concat(t, "张，切换为单张\n将清除已拍名片"),
                    cancelText: "切换单拍",
                    confirmText: "继续连拍",
                    success: function(e) {
                        var t = "multiple";
                        e.cancel && (n.setData({
                            albums: []
                        }), t = "single"), n.triggerEvent("modechange", {
                            mode: t
                        }, {});
                    }
                });
            }
        },
        isSingle: function() {
            return "single" === this.data.mode;
        },
        onChooseAlbum: function() {
            var t = this;
            cardScanner.chooseImage("album", this.isSingle() ? 1 : 9).then(function(e) {
                t.addFile(e), t.triggerEvent("success", {
                    files: e
                });
            });
        },
        onTakePhoto: function() {
            var n = this;
            wx.createCameraContext().takePhoto({
                quality: "normal",
                success: function(e) {
                    var t = [ e.tempImagePath ];
                    n.addFile(t), n.triggerEvent("success", {
                        files: t
                    });
                }
            });
        },
        addFile: function(e) {
            if (!this.isSingle()) {
                var t = this.data.albums.concat(e);
                this.setData({
                    albums: t,
                    toastShown: !0
                }), wx.showToast({
                    title: "已拍".concat(t.length, "张"),
                    duration: 700
                });
            }
        },
        onComplete: function() {
            this.triggerEvent("complete", {});
        },
        onCancel: function() {
            wx.navigateBack({
                delta: 1
            });
        },
        onError: function(e) {
            var t = this;
            (0, _authorize.openSettingModal)("camera", {
                execute: !1,
                fail: function() {
                    wx.showModal({
                        title: "相机授权失败",
                        content: "小程序正常运行需要获得相机功能。请在发现-小程序，删除本小程序后，再次打开，会重新提示微信授信，点“允许”，即可正常使用相机功能...",
                        showCancel: !1,
                        success: function() {
                            return t.triggerEvent("fail", {});
                        }
                    });
                }
            });
        }
    }
});